<?php

class MeetingController extends Controller
{
	public function actionIndex()
	{
		$pid = Yii::app()->request->getParam("pid");
        // get all schedule associate with project
		$modelSchedules = Schedule::model()->findAllByAttributes(array('schedule_project_id' => $pid, 'schedule_type' => "MEETING"));
		
		$schedules = array();
		foreach ($modelSchedules as $modelSchedule) {
			$modelMeeting  = Meeting::model()->findByAttributes(array('meeting_schedule_id' => $modelSchedule->schedule_id));
			$modelMeetingUsers = MeetingUser::model()->findAllByAttributes(array('meeting_user_meeting_id' => $modelMeeting->meeting_id));
			$schedule['modelSchedule'] = $modelSchedule;
			$schedule['modelMeeting'] = $modelMeeting;
			$schedule['modelMeetingUsers'] = $modelMeetingUsers;
			$space  = Html::spaceDate($modelMeeting->meeting_start_date);
			$schedules[$space][] = $schedule;
			//$schedules[] = $schedule;
		}
		$this->render('index', array('schedules' => $schedules));
	}

	// -----------------------------------------------------------
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
	public function actionAdd()
	{
		$pid = Yii::app()->request->getParam('pid');
		if($pid == null) throw new CHttpException(404,'Param not enough');
		
		$meeting = new Meeting();
		$schedule = new Schedule();
		if(!isset($_POST['Meeting']) || !isset($_POST['Schedule'])) {
			$this->render('add', array('meeting' => $meeting, 'schedule' => $schedule));
			return;
		}
		$meeting->attributes = $_POST['Meeting']; 
		$schedule->attributes = $_POST['Schedule'];
		$schedule->schedule_created_date = new CDbExpression('NOW()');
		$schedule->schedule_type = 'MEETING';
		$schedule->schedule_project_id = $pid;
		$schedule->schedule_created_by_user_id = Yii::app()->user->id; 
		
		if($schedule->save()) {
			$meeting->meeting_schedule_id = $schedule->schedule_id;
			if($meeting->save()) {
				// Save meeting user
				if (isset($_POST['meeting_user']))
					foreach ($_POST['meeting_user'] as $userid) {
						$meeting_user = new MeetingUser();
						$meeting_user->meeting_user_meeting_id = $meeting->meeting_id;
						$meeting_user->meeting_user_attending_user_id = $userid;
						$meeting_user->save();
					}
					
				//$this->sendMail($meeting, $_POST['meeting_user']);
				Yii::app()->user->setFlash('success','Schedule meeting successfully');
				$this->redirect(array('/meeting/index','pid'=>$pid));				
			}
		}
		$this->render('add', array('meeting' => $meeting, 'schedule' => $schedule));
		
	}
	
	
	//Edit controller
	public function actionEdit()
	{
		$pid = Yii::app()->request->getParam('pid');
		if($pid == null) throw new CHttpException(404,'Param not enough');
		$mid = Yii::app()->request->getParam('mid');
		
		$meeting = Meeting::model()->findByPk($mid);
		$sid = $meeting->meeting_schedule_id;
		$schedule = Schedule::model()->findByPk($sid);
		
		if(!isset($_POST['Meeting']) || !isset($_POST['Schedule'])) {
			$this->render('edit', array('meeting' => $meeting, 'schedule' => $schedule));
			return;
		}
		$meeting->attributes = $_POST['Meeting']; 
		$schedule->attributes = $_POST['Schedule'];
		$schedule->schedule_updated_date = new CDbExpression('NOW()');
		$schedule->schedule_type = 'MEETING';
		$schedule->schedule_project_id = $pid;
		$schedule->schedule_created_by_user_id = Yii::app()->user->id; 
		
		if($schedule->save()) {
			$meeting->meeting_schedule_id = $schedule->schedule_id;
			if($meeting->save()) {
				// Save meeting user
				if (isset($_POST['meeting_user']))
					foreach ($_POST['meeting_user'] as $userid) {
						$meeting_user = new MeetingUser();
						$meeting_user->meeting_user_meeting_id = $meeting->meeting_id;
						$meeting_user->meeting_user_attending_user_id = $userid;
						$meeting_user->save();
					}
				Yii::app()->user->setFlash('success','Update meeting successfully');
				$this->redirect(array('/meeting/index','pid'=>$pid));				
			}
		}
		$this->render('edit', array('meeting' => $meeting, 'schedule' => $schedule));
		
	}
	
	//Delete meeting
	public function actionDel()
	{
		$pid = Yii::app()->request->getParam('pid');
		$mid = Yii::app()->request->getParam('mid');
		$modelMeeting = Meeting::model()->findByPk($mid);
		$sid = $modelMeeting->meeting_schedule_id;
		$Schedule = new Schedule();
		$Schedule->deleteByPk($sid);
		$this->redirect(array('/meeting/index', 'pid' => $pid));
	}
	
/**
	 * send new message to list user
	 */
	public function sendMail($meeting, $usersId) {
		$SM = Yii::app()->swiftMailer;
		$mailHost = 'localhost';
    		$mailPort = 25; // Optional
    	$content = $this->renderPartial('_mail', array(
        	'meeting' => $meeting,
    	), true);
    	$Transport = $SM->smtpTransport($mailHost, $mailPort);
    	$Mailer = $SM->mailer($Transport);

    	// New message
    	$pid = Yii::app()->request->getParam('pid');
    	$project = Project::model()->findByPk($pid);
    	$sid = $meeting->meeting_schedule_id;
    	$modelSchedule = Schedule::model()->findByPk($sid);
    	$title = "[$project->project_name] {$modelSchedule->schedule_subject}";
    	$Message = $SM
        ->newMessage('My subject')
        ->setFrom(array('xman@runsystem.net' => 'Xmanager'))
        ->addPart($content, 'text/html');
 
    	//Set receiver array
		$receiver = array();
    	foreach ($usersId as $userId) {
    		$user = User::model()->findByPk($userId);
			$receiver[$user->user_email] = $user->user_name;
    	}
		$Message->setTo ($receiver);
		$result = $Mailer->send($Message);
	}
}